/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.data;

import java.text.MessageFormat;

/**
 * A static helper that receive connection parameters and generate a jdbc connection string.
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 *
 * @see     DatabaseType
 * @see     ConnectionString
 * @see     DataConfiguration
 */
public class ConnectionStringHelper {
    /**
     * Generate a jdbc connection string of type databaseType
     *
     * @param databaseType the DatabaseType of the connection string.
     * @param username the username of the connection string.
     * @param password the password of the connection string.
     * @param server the server of the connection string.
     * @param database the database of the connection string.
     * @return
     */
    public static String generate(DatabaseType databaseType, String username, String password, String server, String database) {
        switch (databaseType) {
            case MYSQL: {
                //"jdbc:mysql://192.168.1.202/pluggerdb"
                //"jdbc:mysql://{0}/{1}?user={2}&password={3}"
                return MessageFormat.format("jdbc:mysql://{0}/{1}?user={2}&password={3}",
            			server, database, username, password);
            }
        }
        return null;
    }

    /**
     * Convert a string to DatabaseType.
     *
     * @param value a database type string value.
     * @return a DatabaseType object that represent the value.
     */
    public static DatabaseType toDatabaseType(String value) {
        if (value.equals("mysql")) {
            return DatabaseType.MYSQL;
        }
        if (value.equals("posgresql")) {
            return DatabaseType.POSGRESQL;
        }
        if (value.equals("sqlserver")) {
            return DatabaseType.SQLSERVER;
        }
        if (value.equals("oracle")) {
            return DatabaseType.ORACLE;
        }
        
        return DatabaseType.MYSQL;
    }

    /**
     * Converts a DatabaseType object to a provider string.
     * 
     * @param type DatabaseType to be converted.
     * @return the provider string name.
     */
    public static String getProvider(DatabaseType type) {
        switch (type) {
            case MYSQL:
                return "com.mysql.jdbc.Driver";
            case POSGRESQL:
                return "";
            case SQLSERVER:
                return "";
            case ORACLE:
                return "";
        }
        return null;
    }
}
